if(typeof(starhub) == 'undefined'){
	var starhub = {};
}

starhub.mobile = (function (){
	var opts;
	var bo = {
		init : function (o) {
			opts = o;
			loadProductCarousel();
			loadTiles();
		},
		
		editProductCarousel : function(productId) {
			var param = {
					productId : productId	
			}
			$.ajax({
				url: "/StarHubWeb/editProductCarousel.html",
				cache: false,
				type: 'GET',
				data: param,
				dataType: "html",
				success: function(response) {
					$("#popUpDiv").html(response);
					initClickEvents();
				},
				fail: function(msg) {
					alert("error");
					trace(msg);
				}
			});
			showPopup();
		},
		submitEditProductCarousel : function () {
			
			$.ajax({
				url: "/StarHubWeb/submitEditProductCarousel.html",
				cache: false,
				type: 'POST',
				data: $("#editProductCarouselForm").serializeArray(),
				dataType: "json",
				success: function(response) {
					loadTiles();
				},
				fail: function(msg) {
					alert("error");
					trace(msg);
				}
			});
			loadProductCarousel();
			
			$("#blanket").hide();
			$("#popUpDiv").hide();
		},
		editTile : function(tileId) {
			
//			$("#popUpDiv").attr("src", "/StarHubWeb/editTile.html?tileId=" + tileId);
			var param = {
					tileId : tileId	
			}
			$.ajax({
				url: "/StarHubWeb/editTile.html",
				cache: false,
				type: 'GET',
				data: param,
				dataType: "html",
				success: function(response) {
					$("#popUpDiv").html(response);
					initClickEvents();
				},
				fail: function(msg) {
					alert("error");
					trace(msg);
				}
			});
			showPopup();
		},
		submitEditTile : function () {
			
			$.ajax({
				url: "/StarHubWeb/submitEditTile.html",
				cache: false,
				type: 'POST',
				data: $("#editTileForm").serializeArray(),
				dataType: "json",
				success: function(response) {
					loadTiles();
				},
				fail: function(msg) {
					alert("error");
					trace(msg);
				}
			});
			loadTiles();
			
			$("#blanket").hide();
			$("#popUpDiv").hide();
		},
		hidePopup : function() {
			$("#blanket").hide();
			$("#popUpDiv").hide();
		}
	}
	
	function loadProductCarousel() {
		var param = {
				categoryId : opts.categoryId,
				isAdmin : opts.isAdmin
		};
		$.ajax({
			url: "/StarHubWeb/getProductCarousels.html",
			cache: false,
			type: 'GET',
			data: param,
			dataType: "html",
			success: function(response) {
				$("#producCarouselContainer").html(response);
				handleProductCarousel()
			},
			fail: function(msg) {
				alert("error");
				trace(msg);
			}
		});
	}
	
	function loadTiles() {
		var param = {
				categoryId : opts.categoryId,
				isAdmin : opts.isAdmin
		};
		$.ajax({
			url: "/StarHubWeb/getTiles.html",
			cache: false,
			type: 'GET',
			data: param,
			dataType: "html",
			success: function(response) {
				$("#tilesContainer").html(response);
				initClickEvents();
			},
			fail: function(msg) {
				alert("error");
				trace(msg);
			}
		});
	};
	
	function initClickEvents() {
		$("#nav-tabs-existing-customer-link").click(function() {
			$("#nav-tabs-existing-customer").addClass("active");
			$("#nav-tabs-new-customer").removeClass("active");
			$("#existing-customer").addClass("active");
			$("#new-customer").removeClass("active");
		});
		
		$("#nav-tabs-new-customer-link").click(function() {
			$("#nav-tabs-new-customer").addClass("active");
			$("#nav-tabs-existing-customer").removeClass("active");
			$("#new-customer").addClass("active");
			$("#existing-customer").removeClass("active");
		});
	};
	
	function handleProductCarousel() {
		$(".carousel-inner li.item").first().addClass("active");
		$(".carousel-contents li").first().addClass("active");
		$(".carousel-indicators li").first().addClass("active");
		
		$(".carousel-control.left").click(function () {
			handleRotateLeft(".carousel-inner li.item.active", ".carousel-inner li.item");
			handleRotateLeft(".carousel-contents li.active", ".carousel-contents li");
			handleRotateLeft(".carousel-indicators li.active", ".carousel-indicators li");
		});
		
		$(".carousel-control.right").click(function () {
			handleRotateRight(".carousel-inner li.item.active", ".carousel-inner li.item");
			handleRotateRight(".carousel-contents li.active", ".carousel-contents li");
			handleRotateRight(".carousel-indicators li.active", ".carousel-indicators li");
		});
	};
	
	function handleRotateRight(selector, defaultSelector) {
		var activeObject = $(selector);
		
		if (activeObject.next(defaultSelector).length > 0) {
			activeObject.next(defaultSelector).addClass("active");
		} else {
			$(defaultSelector).first().addClass("active");
		}
		activeObject.removeClass("active");
	};
	
	function showPopup() {
		popup('popUpDiv');
	};
	
	
	
	function handleRotateLeft(selector, defaultSelector) {
		var activeObject = $(selector);
		
		if (activeObject.prev(defaultSelector).length > 0) {
			activeObject.prev(defaultSelector).addClass("active");
		} else {
			$(defaultSelector).last().addClass("active");
		}
		activeObject.removeClass("active");
	}
	
	return bo;
}());